<?
/**
 *
 * @category   Mojito
 * @package    Mojito_Model
 * @author	   Richard A. Jones (twitter.com/talentedmrjones)
 * @copyright  Copyright (c) 2009 Richard A. Jones
 * @license 
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the author .
 */

/**
 * Abstract Mojito_Model_Abstract class.
 * 
 * @abstract
 */
abstract class Mojito_Model_Abstract
{
	/**
	 * _errors
	 * 
	 * (default value: array())
	 * 
	 * @var array
	 * @access protected
	 */
	protected $_errors=array();
	
	/**
	 * _message
	 * 
	 * (default value: '')
	 * 
	 * @var string
	 * @access protected
	 */
	protected $_message='';
	
	/**
	 * _dbTableClass
	 * 
	 * (default value: null)
	 * 
	 * @var mixed
	 * @access protected
	 */
	protected $_dbTableClass=null;
	
	/**
	 * _DbTable
	 * 
	 * (default value: null)
	 * 
	 * @var mixed
	 * @access protected
	 */
	protected $_DbTable=null;
	
	/**
	 * __construct function.
	 * 
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		if (!empty($this->_dbTableClass)) $this->_DbTable = new $this->_dbTableClass(array('db'=>Mojito_Model::getDbAdapter()));
		$this->init();
	}
	
	/**
	 * addError function.
	 * 
	 * @access protected
	 * @param mixed $error
	 * @return false
	 */
	protected function addError($error)
	{
		$this->_errors[]=$error;
		return false;
	}

		
	/**
	 * getErrors function.
	 * 
	 * @access public
	 * @return array
	 */
	public function getErrors()
	{return $this->_errors;}
	
	/**
	 * getMessage function.
	 * 
	 * @access public
	 * @return void
	 */
	public function getMessage()
	{return $this->_message;}
	
	/**
	 * setMessage function.
	 * 
	 * @access public
	 * @param mixed $msg
	 * @return void
	 */
	public function setMessage($msg)
	{$this->_message=$msg;}
	
	/**
	 * isValidId function.
	 * defaults to test for non-empty numeric values. Override to declare something custom
	 * @access protected
	 * @param int $id
	 * @return boolean
	 */
	protected function isValidId($id)
	{
		if (!empty($id)&&is_numeric($id)) return true;
		else return false;
	}
	
	/**
	 * create function.
	 * 
	 * @access public
	 * @param mixed $params
	 * @return void
	 */
	public function create($params)
	{
		$Row=$this->_DbTable->createRow($params);
		$Row->save();
		return $Row;
	}

	
	/**
	 * getAll function.
	 * 
	 * @access public
	 * @return void
	 */
	public function getAll()
	{
		return $this->verifyRowset($this->_DbTable->fetchAll());
	}
	
	/**
	 * getById function.
	 * 
	 * @access public
	 * @param mixed $id
	 * @return void
	 */
	public function getById($id)
	{
		if (!$this->isValidId($id)) return false;
		else return $this->verifyRow($this->_DbTable->find($id)->current());
	}
				
	/**
	 * verifyRow function.
	 * 
	 * @access public
	 * @param mixed $Row. (default: null)
	 * @return void
	 */
	public function verifyRow($Row=null)
	{
		if (!$Row instanceof Zend_Db_Table_Row_Abstract) return false;
		else return $Row;
	}

	/**
	 * verifyRowset function.
	 * 
	 * @access public
	 * @param mixed $Rowset. (default: null)
	 * @return void
	 */
	public function verifyRowset($Rowset=null)
	{
		if ($Rowset instanceof Zend_Db_Table_Rowset_Abstract && $Rowset->count()>0) return $Rowset;
		else return false;
	}

	/**
	 * model function.
	 * proxies to Mojito_Model::get() providing a way to access other models via $this
	 * @access protected
	 * @param mixed $model
	 * @return object Mojito_Model_Abstract
	 */
	protected function model($model)
	{
		return Mojito_Model::get($model);
	}
	
	/**
	 * init function.
	 * 
	 * @access protected
	 * @return void
	 */
	protected function init(){}
	
	protected function getDbAdapter()
	{
		return Mojito_Model::getDbAdapter();
	}
	
	protected function getLog()
	{
		return Mojito_Model::getLog();
	}
	
	protected function getRowset(array $rows)
	{
		$data  = array(
            'table'    => $this->_DbTable,
            'data'     => $rows,
            'readOnly' => false,
            'rowClass' => $this->_DbTable->getRowClass(),
            'stored'   => true
        );

        $rowsetClass = $this->_DbTable->getRowsetClass();
        if (!class_exists($rowsetClass)) {
            require_once 'Zend/Loader.php';
            Zend_Loader::loadClass($rowsetClass);
        }
        return new $rowsetClass($data);
	}

}